package com.runme.chatrobot.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by nivalsoul on 2015/10/29.
 */
public class HttpUtil {
    public static String doGet (String urlWithParam){
        String str = null;
        InputStream in=null;
        BufferedReader bufReader=null;
        try {
            URL uri=new URL(urlWithParam);
            HttpURLConnection urlConnection= (HttpURLConnection) uri.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setRequestProperty("Connection", "close");
            in=urlConnection.getInputStream();
            bufReader=new BufferedReader(new InputStreamReader(in,"utf-8"));
            StringBuffer strBuffer=new StringBuffer();
            char[] data=new char[128];
            while (bufReader.read(data)>-1){
                strBuffer.append(data);
            }
            return  str=strBuffer.toString();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (bufReader!=null){
                try {
                    bufReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return  str;
    }

}
